import React, { useState, useEffect } from 'react';
import { connect } from 'dva';
import EditTemplate from '@/components/EditTemplate';
const BuildEdit = ({ location, dispatch, dict }: any) => {
  const [detail, setDetail] = useState(null);
  const [approve, setapprove] = useState(null);
  const { id } = location.query;

  useEffect(() => {
    if (id) {
      // 项目基本信息
      dispatch({
        type: '$request/post',
        payload: {
          url: '/api/scheme/v1/detail',
          data: {
            uuid: id,
            page: 0,
            size: 10,
          },
        },
      }).then(res => {
        res.buildType = res.project.buildType == 'BUILD' ? '建设' : '保障';
        setDetail(res);
      });

      // 评审信息
      // dispatch({
      //   type: '$request/post',
      //   payload: {
      //     url: '/api/examine/v1/record/detail',
      //     data: {
      //       uuid: id,
      //       page: 0,
      //       size: 10,
      //     },
      //   },
      // }).then(res => {
      //   setapprove(res);
      // });
    }
  }, []);
  const filters = [
    {
      title: '基础信息',
      list: [
        {
          key: ['project', 'name'],
          type: 'text',
          label: '项目名称',
          span: 12,
        },
        {
          key: ['project', 'no'],
          type: 'text',
          label: '项目ID',
          span: 12,
        },
        {
          key: ['project', 'creator', 'username'],
          type: 'text',
          label: '业主单位',
          span: 12,
        },
        {
          key: ['project', 'professionData', 'display'],
          type: 'text',
          label: '行业领域',
          span: 12,
        },
        {
          key: 'buildType',
          type: 'text',
          label: '项目性质',
          span: 12,
          options: dict.buildType,
        },
        {
          key: ['project', 'serviceTypeData', 'display'],
          type: 'text',
          label: '项目类型',
          span: 24,
        },
        {
          key: 'name',
          type: 'text',
          label: '方案名称',
          span: 12,
        },
        {
          key: 'org',
          type: 'text',
          label: '方案设计单位',
          span: 12,
        },
        {
          key: 'budget',
          type: 'text',
          label: '项目投资概算',
          span: 24,
        },
        {
          key: 'fileList',
          type: 'upload',
          label: '方案电子材料',
          rules: [{ required: true }],
          span: 24,
        },
      ],
    },
  ];

  const approveFilters = [
    {
      title: '评审信息',
      list: [
        {
          key: 'examineOrgUuidList',
          type: 'select',
          label: '评审机构',
          mode: 'multiple',
          asyncopt: {
            url: '/api/examine/v1/org/query',
          },
          span: 24,
        },
        {
          key: 'examinePersonUuidList',
          type: 'select',
          label: '评审专家',
          mode: 'multiple',
          asyncopt: {
            url: '/api/examine/v1/person/query',
          },
          span: 24,
        },
        {
          key: 'date',
          type: 'datepicker',
          label: '评审日期',
          rules: [{ required: true }],
          span: 12,
        },
        {
          key: 'address',
          type: 'input',
          label: '评审地点',
          rules: [{ required: true }],
          span: 12,
        },
        {
          key: 'suggestion',
          type: 'textarea',
          label: '评审意见',
          span: 24,
        },
        {
          key: 'changeAmount',
          type: 'input',
          label: '评审增减金额',
          placeholder: '增加金额输入正数，减少金额输入负数',
          span: 24,
          prop: {
            addonAfter: '万元',
          },
          rules: [
            {
              required: true,
              pattern: new RegExp(/^-?\d+/, 'g'),
              message: '只能输入数字',
            },
          ],
        },
        {
          key: 'fileList',
          type: 'upload',
          label: '评审电子材料',
          span: 24,
        },
      ],
    },
  ];
  const preproccess = data => {
    const fileUuidList = data.fileList
      ? data.fileList.map(item => item.uuid)
      : [];
    return {
      fileUuidList,
      schemeUuid: id,
      ...data,
    };
  };
  return (
    <div>
      <EditTemplate
        filters={filters}
        initialValues={detail}
        readOnly={true}
        formLayout={{
          labelCol: { span: 6 },
        }}
      />
      <EditTemplate
        filters={approveFilters}
        initialValues={approve}
        updateUrl="/api/examine/v1/record/create"
        formLayout={{
          labelCol: { span: 6 },
        }}
        preproccess={preproccess}
      />
    </div>
  );
};

export default connect((state: any) => state.base)(BuildEdit);
